package com.angel.oss;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;

public class FileOperation {
	//内网 ：tianshidata.oss-cn-qingdao-internal.aliyuncs.com
	// TODO Auto-generated method stub
	// endpoint以杭州为例，其它region请按实际情况填写
	static String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
//	static String endpoint = "http://tianshidata.oss-cn-qingdao-internal.aliyuncs.com";
	// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录
	// https://ram.console.aliyun.com 创建
	static String accessKeyId = "LTAIH0ihGo2wWa0U";
	static String accessKeySecret = "YCEUufhLNc135ZqtdapkR0tYz3gs77";
	static String bucketName = "tianshidata";
	
	// http://tianshidata.oss-cn-qingdao.aliyuncs.com/test/java-123.doc
	public static void main(String[] args) throws Exception {
		String key = "test/java-123.doc";
		String des = "c:/tmp/java-123-2.doc";
		DownLoadFile(key, des);
	}
	
	public static File DownLoadFile(String key, String des) throws Exception {
		long t1 = System.currentTimeMillis();
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		OSSObject ossObject = ossClient.getObject(bucketName, key);
		File file = new File(des);
		InputStream is = ossObject.getObjectContent();
		OutputStream os = new FileOutputStream(file);
		byte[] b = new byte[10000];
		int bytesRead = 0;
		while ((bytesRead = is.read(b)) != -1) {
			os.write(b, 0, bytesRead);
		}
		os.flush();
		os.close();
		is.close();
		ossClient.shutdown();
		System.out.println("下载时间：" + (System.currentTimeMillis() - t1) + "ms");
		return file;
	}

	public static void UpLoadFile(String sou, String des) {
	 
			// 创建OSSClient实例
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			// 上传文件
			//
			long t1 = System.currentTimeMillis();
			ossClient.putObject(bucketName, des, new File(sou));
			// 关闭client
			ossClient.shutdown();
			System.out.println("上传时间：" + (System.currentTimeMillis() - t1) + "ms");
			
	}

	/**
	 * @param key
	 *            "test/java-123.doc"
	 * @throws IOException
	 */
	public void fileReader(String key) throws IOException {

		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		OSSObject ossObject = ossClient.getObject(bucketName, key);
		// 读Object内容
		System.out.println("Object content:");
		BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
		while (true) {
			String line = reader.readLine();
			if (line == null)
				break;
			System.out.println("\n" + line);
		}
		reader.close();
		ossObject.getObjectContent().close();
		// 关闭client
		ossClient.shutdown();

	}

}
